package com.wh.base.sys.config.controller;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wh.base.aop.documented.OperLog;
import com.wh.base.sys.config.controller.form.IdForm;
import com.wh.base.sys.config.controller.form.IdsForm;
import com.wh.base.sys.config.controller.form.InsertForm;
import com.wh.base.sys.config.controller.form.SearchListForm;
import com.wh.base.sys.config.controller.form.SearchPageForm;
import com.wh.base.sys.config.controller.form.UpdateForm;
import com.wh.base.sys.config.entity.Config;
import com.wh.base.sys.config.service.ConfigService;
import com.wh.base.sys.user.service.UserService;
import com.wh.base.util.R;
import com.wh.base.util.RedisUtil;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 系统配置 前端控制器
 * </p>
 *
 * @author WangHong
 * @since 2022-04-14
 */
@Controller
@RequestMapping("/sys/config")
public class ConfigController {
	@Autowired
	private ConfigService service;

	@Autowired
	private UserService userService;
	
	@Resource
    private RedisUtil redisUtil;

	@GetMapping("/index")
	@ApiOperation(value = "首页页面")
	@SaCheckPermission("config:view")
	public String index(Model model){
		model.addAttribute("authSign", (Map<String,String>)redisUtil.get("auth_sign_"+StpUtil.getLoginIdAsLong())); //用户权限
		return "config/index";
	}
	
	@GetMapping("/recycleIndex")
	@ApiOperation(value = "回收站页面")
	@SaCheckPermission("config:recycle")
	public String recycleIndex(Model model){
		model.addAttribute("authSign", (Map<String,String>)redisUtil.get("auth_sign_"+StpUtil.getLoginIdAsLong())); //用户权限
		return "config/recycle";
	}

	@PostMapping("/getPage")
	@ApiOperation(value = "分页查询数据")
	@ResponseBody
	@SaCheckLogin
	public R getPage(@Validated SearchPageForm form){
		Page roadPage = new Page<>(form.getPageNum(), form.getPageSize());
		LambdaQueryWrapper<Config> queryWrapper = new QueryWrapper().lambda();
		queryWrapper.eq(Config::getDeleteFlag, "0");
		if(form.getName() != null) {
			queryWrapper.like(Config::getName, form.getName());
		}
		Page pageList = service.page(roadPage, queryWrapper);
		return R.ok().put("page", pageList);
	}

	@PostMapping("/getList")
	@ApiOperation(value = "查询所有数据")
	@ResponseBody
	@SaCheckLogin
	public R getList(@Validated SearchListForm form){
		LambdaQueryWrapper<Config> queryWrapper = new QueryWrapper().lambda();
		queryWrapper.eq(Config::getDeleteFlag, "0");
		if(form.getName() != null) {
			queryWrapper.like(Config::getName, form.getName());
		}
		return R.ok().put("list", service.list(queryWrapper));
	}

	@PostMapping("/getRecyclePage")
	@ApiOperation(value = "分页查询回收站数据")
	@ResponseBody
	@SaCheckPermission("config:recycle")
	public R getRecyclePage(@Validated SearchPageForm form){
		Page roadPage = new Page<>(form.getPageNum(), form.getPageSize());
		LambdaQueryWrapper<Config> queryWrapper = new QueryWrapper().lambda();
		queryWrapper.eq(Config::getDeleteFlag, "1");
		if(form.getName() != null) {
			queryWrapper.like(Config::getName, form.getName());
		}
		Page pageList = service.page(roadPage, queryWrapper);
		return R.ok().put("page", pageList);
	}

	@PostMapping("/getById")
	@ApiOperation(value = "根据id查询数据")
	@SaCheckPermission("config:view")
	@ResponseBody
	public R getById(@Validated IdForm form){
		return R.ok().put("obj", service.getById(form.getId()));
	}

	@PostMapping("/add")
	@ApiOperation(value = "添加数据")
	@SaCheckPermission("config:add")
	@ResponseBody
	@OperLog(operModul = "配置管理", operType = "新增" ,operDesc = "新增系统配置")
	public R add(@Validated InsertForm form){
		Config config = JSONUtil.parse(form).toBean(Config.class);
		config.setCreateTime(new Date());
		config.setAllowedUpdate("0");
		config.setCreateUser(userService.getUser().getUsername());
		if(service.save(config)){
			return R.ok("添加成功");
		}else{
			return R.error("添加失败");
		}

	}

	@PostMapping("/update")
	@ApiOperation(value = "修改数据")
	@SaCheckPermission("config:update")
	@ResponseBody
	@OperLog(operModul = "配置管理", operType = "修改" ,operDesc = "修改系统配置")
	public R update(@Validated UpdateForm form){
		//检查是否存在不能修改的数据
		if(service.checkAllowedUpdate(Arrays.asList(form.getId()))) {
			return R.error("数据禁止修改");
		}
		Config config = JSONUtil.parse(form).toBean(Config.class);
		config.setUpdateTime(new Date());
		config.setUpdateUser(userService.getUser().getUsername());
		service.saveOrUpdate(config);
		return R.ok("修改成功");
	}

	@PostMapping("/restore")
	@ApiOperation(value = "根据ids批量还原回收站的数据")
	@SaCheckPermission("config:recycle")
	@ResponseBody
	@OperLog(operModul = "配置管理", operType = "还原回收站配置" ,operDesc = "还原回收站配置")
	public R restore(@Validated IdsForm form){
		List<Long> idList = form.idList();
		if(idList.size() > 0) {
			List<Config> configList = new ArrayList<>();
			for(Long id : idList) {
				Config config = new Config();
				config.setId(id);
				config.setDeleteFlag("0");
				config.setUpdateTime(new Date());
				config.setUpdateUser(userService.getUser().getUsername());
				configList.add(config);
			}
			if(configList.size() > 0) {
				service.saveOrUpdateBatch(configList);
			}
		}
		return R.ok("还原成功");
	}

	@PostMapping("/recycle")
	@ApiOperation(value = "根据id集合将数据放入回收站")
	@SaCheckPermission("config:recycle")
	@ResponseBody
	@OperLog(operModul = "配置管理", operType = "放入回收站" ,operDesc = "将配置放入回收站")
	public R recycle(@Validated IdsForm form){
		List<Long> idList = form.idList();
		//检查是否存在不能修改的数据
		if(service.checkAllowedUpdate(idList)) {
			return R.error("存在禁止修改的数据");
		}
		if(idList.size() > 0) {
			List<Config> configList = new ArrayList<>();
			for(Long id : idList) {
				Config config = new Config();
				config.setId(id);
				config.setDeleteFlag("1");
				config.setUpdateTime(new Date());
				config.setUpdateUser(userService.getUser().getUsername());
				configList.add(config);
			}
			if(configList.size() > 0) {
				service.saveOrUpdateBatch(configList);
			}
		}
		return R.ok("删除成功");
	}

	@PostMapping("/delete")
	@ApiOperation(value = "根据id集合彻底删除数据")
	@SaCheckPermission("config:delete")
	@ResponseBody
	@OperLog(operModul = "配置管理", operType = "彻底删除" ,operDesc = "彻底删除配置")
	public R delete(@Validated IdsForm form){
		List<Long> idList = form.idList();
		//检查是否存在不能修改的数据
		if(service.checkAllowedUpdate(idList)) {
			return R.error("存在禁止修改的数据");
		}
		if(idList.size() > 0) {
			service.removeByIds(idList);
		}
		return R.ok("删除成功");
	}

}
